import {
    GetStorePrincipalInfo,
    BindStoreOpen

} from '@/services/wechat/weixin';
import {catchException} from '@/utils/utils';

export default {
    namespace: 'wechatPrincipal',

    state: {
        principalInfo: [],
    },

    effects: {

        // 获取主体信息
        *GetStorePrincipalInfo({ params, callBack }, { call, put }) {

            const data = yield call(GetStorePrincipalInfo);
            yield catchException(function* (){
              if (data&&data.Code === 0) {
                let array = []
                let info = {
                    key:'name',
                    tit: '主体名称',
                    name: data.Data.PrincipalName,
                    info: '主体不能修改，重新授权其它公众号或小程序时，都必须是此主体下的帐号。',
                    isopen: false,
                    IsBinded: false
                }
                array.push(info)
                let open = {
                    key:'num',
                    tit: '开放平台帐户',
                    name: data.Data.OpenId,
                    info: '公众号和小程序在同一开放平台帐号下，买家可以通过授权实现帐户互通',
                    isopen: true,
                    IsBinded: data.Data.IsBinded
                }
                array.push(open)
                yield put({
                    type: 'StorePrincipalInfo',
                    info: array,
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data);
              }
            })

        },
        //绑定主体信息
        *BindStoreOpen({ params, callBack }, { call, put }) {
            const data = yield call(BindStoreOpen, params);
            yield catchException(function* (){
              if (data&&data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                callBack && callback.error(data)
              }
            })

        },

    },

    reducers: {
        StorePrincipalInfo(state, { info }) {
            return {
                ...state,
                principalInfo: info,
            };
        },

    }
}
